#ifndef __M_ROUTE_H
#define __M_ROUTE_H

#include <iostream>
#include "../mqcommon/mq_logger.hpp"
#include "../mqcommon/mq_helper.hpp"
#include "../mqcommon/mq_msg.pb.h"

namespace lywmq
{

class Router
{
public: 
    static bool isLegalRoutingKey(const std::string& routing_key)
    {
        // a-z, A-Z, 0-9, ., _
        for (auto& ch : routing_key)
        {
            if ((ch >= 'a' && ch <= 'z') ||
                (ch >= 'A' && ch <= 'Z') ||
                (ch >= '0' && ch <= '9') ||
                (ch == '_' || ch == '.'))
                continue;

            return false;
        }
        return true;
    }
    static bool isLegalBindingKey(const std::string& binding_key)
    {
        // a-z, A-Z, 0-9, ., _, *, #
        for (auto& ch : binding_key)
        {
            if ((ch >= 'a' && ch <= 'z') ||
                (ch >= 'A' && ch <= 'Z') ||
                (ch >= '0' && ch <= '9') ||
                (ch == '_' || ch == '.') ||
                (ch == '*' || ch == '#'))
                continue;

            return false;
        }
        // *和#必须独立存在
        std::vector<std::string> sub_words;
        StrHelper::split(binding_key, ".", sub_words);
        for (auto& word : sub_words)
        {
            if (word.size() > 1 && (word.find("*") != std::string::npos || word.find("#") != std::string::npos))
                return false;
        }
        // *和#不能连续出现
        for (int i = 1; i < sub_words.size(); i++)
        {
            if ((sub_words[i] == "#" && sub_words[i-1] == "*") ||
                (sub_words[i] == "#" && sub_words[i-1] == "#") ||
                (sub_words[i] == "*" && sub_words[i-1] == "#"))
                return false;
        }
        return true;
    }
    static bool route(ExchangeType type, const std::string& routing_key, const std::string& binding_key)
    {
        if (type == ExchangeType::DIRECT)
        {
            return (routing_key == binding_key);
        }
        else if (type == ExchangeType::FANOUT) // 广播交换
        {
            return true;
        }
        else if (type == ExchangeType::TOPIC) // 主题交换（动态规划）
        {
            std::vector<std::string> bkeys, rkeys;
            int n_bkey = StrHelper::split(binding_key, ".", bkeys);
            int n_rkey = StrHelper::split(routing_key, ".", rkeys);
            std::vector<std::vector<bool>> dp(n_bkey + 1, std::vector<bool>(n_rkey + 1, false));
            // 初始化
            dp[0][0] = true; 
            for (int i = 1; i <= n_bkey; i++)
            {
                if (bkeys[i - 1] == "#")
                {
                    dp[i][0] = true;
                    continue;
                }
                break;
            }
            // 匹配过程
            for (int i = 1; i <= n_bkey; i++)
            {
                for (int j = 1; j <= n_rkey; j++)
                {
                    
                    if (bkeys[i-1] == rkeys[j-1] || bkeys[i-1] == "*")
                    {
                        // bkeys是*或者单词匹配成功，从左上方继承结果
                        dp[i][j] = dp[i-1][j-1];
                    }
                    else if (bkeys[i-1] == "#")
                    {
                        // bkeys是#则从左，左上，正上继承结果
                        dp[i][j] = dp[i-1][j-1] | dp[i][j-1] | dp[i-1][j];
                    }
                }
            }
            return dp[n_bkey][n_rkey];
        }
        return true;
    }
private:
};

}

#endif